package com.yymagicer.config;

import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;

import java.util.Calendar;
import java.util.Collection;
import java.util.Date;

/**
 * <p>Description: some description </p>
 *
 * @author : xiaodong.yang
 * @date : 2023/12/22 17:18
 */
public class DatePreciseShardingAlgorithm implements PreciseShardingAlgorithm<Date> {
    /**
     * 精确匹配查询
     *
     * @param collection           数据库中所有的事实表
     * @param preciseShardingValue 分片相关信息
     * @return 返回匹配的数据源
     */
    @Override
    public String doSharding(Collection<String> collection, PreciseShardingValue<Date> preciseShardingValue) {
        String logicTableName = preciseShardingValue.getLogicTableName();
        // 匹配满足当前分片规则的表名称
        Date date = preciseShardingValue.getValue();
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int year = cal.get(Calendar.YEAR);
        return logicTableName + "_" + year;
    }
}
